import axios from 'axios';

/* ajax-get */
export const fetch = (url,data={}) =>{
	return new Promise((resolve,reject) => {
		axios({
			method:'get',
			url:url,
            params:data,
            headers: {
                token: sessionStorage.getItem('token')
            }
		}).then(resp=>{
			resolve(resp)
		},reject)
	})
}

/* json-post */
export const postJson = (url,data={}) =>{
	return new Promise((resolve,reject)=>{
		axios({
			method:'post',
			url:url,
            data:data,
            headers: {
                token: sessionStorage.getItem('token')
            }
		}).then(resp=>{
			resolve(resp)
		},reject)
	})
}

/* ajax-post */
export const postData = (url,data={}) =>{
	return new Promise((resolve,reject)=>{
		axios({
			method:'post',
			url:url,
			headers:{
                'Content-type': 'application/x-www-form-urlencoded',
                token: sessionStorage.getItem('token')
			},
			data:data,
			transformRequest: [function (data) {
				let ret = ''
				for (let it in data) {
				  ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
				}
				return ret
			  }],
		}).then(resp=>{
			resolve(resp)
		},reject)
	})
}
